<?php

$api_lang = array(
'name'=>'支付宝通用登录',
'app_key'=>'合作者身份ID',
'app_secret'=>'安全检验码',
);
$config = array(
'app_key'=>array(
'INPUT_TYPE'=>'0',
),
'app_secret'=>array(
'INPUT_TYPE'=>'0'
),
);
if (isset($read_modules) &&$read_modules == true)
{
if(ACTION_NAME=='install')
{
$GLOBALS['db']->query('ALTER TABLE `'.DB_PREFIX.'user`  ADD COLUMN `alipay_id`  varchar(255) NOT NULL','SILENT');
}
$module['class_name']    = 'Alipay';
$module['name']    = $api_lang['name'];
$module['config'] = $config;
$module['lang'] = $api_lang;
return $module;
}
require_once(APP_ROOT_PATH.'system/libs/api_login.php');
class Alipay_api implements api_login {
private $api;
public function __construct($api)
{
$api['config'] = unserialize($api['config']);
$this->api = $api;
}
public function get_api_url()
{
require_once APP_ROOT_PATH.'system/api_login/alipay/alipay_user_service.php';
$callback = get_domain().APP_ROOT.'/api_callback.php?c=Alipay';
es_session::start();
$parameter = array(
'service'=>'user_authentication',
'partner'=>$this->api['config']['app_key'],
'return_url'=>$callback,
'_input_charset'=>'utf-8',
);
$alipay = new alipay_user_service($parameter,$this->api['config']['app_secret'],'MD5');
$sHtmlText = $alipay->build_form($this->api['icon']);
return $sHtmlText;
}
public function get_big_api_url()
{
require_once APP_ROOT_PATH.'system/api_login/alipay/alipay_user_service.php';
$callback = get_domain().APP_ROOT.'/api_callback.php?c=Alipay';
es_session::start();
$parameter = array(
'service'=>'user_authentication',
'partner'=>$this->api['config']['app_key'],
'return_url'=>$callback,
'_input_charset'=>'utf-8',
);
$alipay = new alipay_user_service($parameter,$this->api['config']['app_secret'],'MD5');
$sHtmlText = $alipay->build_form($this->api['bicon']);
return $sHtmlText;
}
public function callback()
{
require_once APP_ROOT_PATH.'system/api_login/alipay/alipay_notify.php';
es_session::start();
$alipay = new alipay_notify($this->api['config']['app_key'],$this->api['config']['app_secret'],'MD5','utf-8','http');
$verify_result = $alipay->return_verify();
if($verify_result) {
$user_id           = $_GET['user_id'];
$msg['id'] = $user_id;
$msg['name'] = 'ali_'.$user_id;
$msg['field'] = 'alipay_id';
es_session::set('api_user_info',$msg);
$user_data = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'user where alipay_id = '.$msg['id'].' and alipay_id <> 0');
if($user_data)
{
$user_current_group = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'user_group where id = '.intval($user_data['group_id']));
$user_group = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'user_group where score <='.intval($user_data['score']).' order by score desc');
if($user_current_group['score']<$user_group['score'])
{
$user_data['group_id'] = intval($user_group['id']);
}
es_session::set('user_info',$user_data);
$GLOBALS['db']->query('update '.DB_PREFIX."user set login_ip = '".get_client_ip()."',login_time= ".get_gmtime().',group_id='.intval($user_data['group_id']).' where id ='.$user_data['id']);
$GLOBALS['db']->query('update '.DB_PREFIX.'deal_cart set user_id = '.intval($_SESSION['user_info']['id'])." where session_id = '".es_session::id()."'");
es_session::delete('api_user_info');
app_recirect_preview();
}
else
app_redirect(url('shop','user#api_login'));
}
else {
echo 'fail';
}
exit;
}
public function get_title()
{
return '支付宝通用登录';
}
public function create_user()
{
$s_api_user_info = es_session::get('api_user_info');
$user_data['user_name'] = $s_api_user_info['name'];
$user_data['user_pwd'] = md5(rand(100000,999999));
$user_data['create_time'] = get_gmtime();
$user_data['update_time'] = get_gmtime();
$user_data['login_ip'] = get_client_ip();
$user_data['group_id'] = $GLOBALS['db']->getOne('select id from '.DB_PREFIX.'user_group order by score asc limit 1');
$user_data['is_effect'] = 1;
$user_data['alipay_id'] = $s_api_user_info['id'];
$count = 0;
do{
if($count>0)
$user_data['user_name'] = $user_data['user_name'].$count;
if(intval($user_data['alipay_id'])>0)
$GLOBALS['db']->autoExecute(DB_PREFIX.'user',$user_data,'INSERT','','SILENT');
$rs = $GLOBALS['db']->insert_id();
$count++;
}while(intval($rs)==0&&intval($user_data['alipay_id'])>0);
$user_info = $GLOBALS['db']->getRow('select * from '.DB_PREFIX.'user where id = '.intval($rs));
es_session::set('user_info',$user_info);
es_session::delete('api_user_info');
}
}

?>